package com.example.news.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.news.Acg.AcgAdapter;
import com.example.news.Acg.AcgUrl;
import com.example.news.Video.MyLayoutManager;
import com.example.news.R;


import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;



public class AcgFragment extends Fragment {
    ExecutorService executor = Executors.newFixedThreadPool(2);
    ThreadPoolExecutor pool = (ThreadPoolExecutor) executor;
    OkHttpClient okHttpClient = new OkHttpClient();
    private View view;
    private RecyclerView rvMainAcg;
    private AcgAdapter acgAdapter;
    private List<AcgUrl> list = new ArrayList<>();



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.acgfragment, container, false);
        super.onCreate(savedInstanceState);

        for (int i =0; i < 50; i++) {
            pool.execute(new task());
        }

        initView();
        return view;
    }



    private void initView() {
        rvMainAcg = view.findViewById(R.id.rv_main_acg);
        //materialRefreshLayout=view.findViewById(R.id.refresh);
        acgAdapter = new AcgAdapter(getActivity(),list);

        rvMainAcg.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //rvMainAcg.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
//
        rvMainAcg.setLayoutManager(new MyLayoutManager(getActivity(), OrientationHelper.VERTICAL, false));
        rvMainAcg.setAdapter(acgAdapter);

//        materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
//            @Override
//            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
//                for (int i =0; i < 2; i++){
//                    pool.execute(new task());
//                }
//                //acgAdapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
//                for (int i =0; i < 2; i++){
//                    pool.execute(new task());
//                }
//                    //acgAdapter.notifyDataSetChanged();
//            }
//        });

    }





    private class task implements Runnable{

        @Override
        public void run() {
            try {
                TimeUnit.MILLISECONDS.sleep(500);
                //acgAdapter.notifyDataSetChanged();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Request request = new Request.Builder()
                    .url("http://api.repeater.vip/cos-img")
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String result = response.body().string();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            list.add(new AcgUrl(result));
                            acgAdapter.notifyDataSetChanged();
                        }
                    });

                }
            });
            Log.i("TAG", "run: "+list.size());
        }

    }




    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.e("TAG", "FourFragment onHiddenChanged :" + hidden);

    }

}
